<?php
/*
 * 用户组
 */
namespace app\admin\controller;
use think\Db;
use think\Loader;
class Group extends Comm
{
	public function index(){
		$list=Db::name('auth_group')->select();
		$this->assign('list', $list);
        $this->assign('nav', array('user', 'grouplist', 'grouplist'));//导航
        return $this->fetch();
	}
	//删除
	public function del(){
		$data=input('post.');
		$ids=isset($data['ids']) ? $data['ids'] : false;
		if(is_array($ids)){ 
			foreach($ids as $k => $v){
				 $ids[$k] = intval($v);
			}
			$ids = implode(',', $ids);
            $map['id'] = array('in', $ids);
            if(Db::name('auth_group')->where($map)->delete()){
            	addlog('删除用户组ID：' . $ids);
                //$this->success('恭喜，用户组删除成功！');
				return json(array('code' => 200, 'msg' => '恭喜，用户组删除成功！'));
            }else {
                $this->error('参数错误！');
            }
		}else{
			$this->error('参数错误！');
		}
	}
	
	public function add(){
		//获取所有启用的规则
        $rule = Db::name('auth_rule')->field('id,pid,title')->where('status=1')->order('o asc')->select();
        $rule = $this->getMenu($rule);
        $this->assign('rule', $rule);
        return $this->fetch('form');
	}
	//提交新增
	public function update(){
		$data['title'] = isset($_POST['title']) ? trim($_POST['title']) : false;
        $id = isset($_POST['id']) ? intval($_POST['id']) : false;
        if ($data['title']) {
            $status = isset($_POST['status']) ? $_POST['status'] : '';
            if ($status == 1) {
                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            $rules= isset($_POST['rules']) ? $_POST['rules'] : 0;
			if($rules){
				foreach($rules as $k => $v){
					 $rules[$k] = intval($v);
				}
			    $rules= implode(',', $rules);
            	$data['rules']=$rules;	
			}
            //var_dump($rules);exit;
             if ($id) {
                $group = Db::name('auth_group')->where('id=' . $id)->update($data);
                if ($group) {
                    addlog('编辑用户组，ID：' . $id . '，组名：' . $data['title']);
                    $this->success('恭喜，用户组修改成功！','index');
                    exit(0);
                } else {
                    $this->success('未修改内容');
                }
            } else {
                Db::name('auth_group')->insert($data);
                addlog('新增用户组，ID：' . $id . '，组名：' . $data['title']);
                $this->success('恭喜，新增用户组成功！','index');
                exit(0);
            }
          } else {
            $this->error('用户组名称不能为空！');
        }
	}
	//编辑用户组
	public function edit(){
		$id = input('param.id',false, 'strip_tags');
        if (!$id) {
            $this->error('参数错误！');
        }
        $group = Db::name('auth_group')->where('id',$id)->find();
        if (!$group) {
            $this->error('参数错误！');
        }
        //获取所有启用的规则
        $rule = Db::name('auth_rule')->field('id,pid,title')->where('status=1')->order('o asc')->select();
        $group['rules'] = explode(',', $group['rules']);
        $rule = $this->getMenu($rule);
        $this->assign('rule', $rule);
        $this->assign('group', $group);
        $this->assign('nav', array('user', 'grouplist', 'addgroup'));//导航
        return $this->fetch('form');
	}
	
    public function status()
    {

        $id = input('id');
        if (!$id) {
            $this->error('参数错误！');
        }
        if ($id == 1) {
            $this->error('此用户组不可变更状态！');
        }
        $group = db('auth_group')->where('id=' . $id)->find();
        if (!$group) {
            $this->error('参数错误！');
        }
        $status = $group['status'];
        if ($status == 1) {
           $res = db('auth_group')->data(array('status' => 0))->where('id=' . $id)->update();
        }
        if ($status != 1 ) {
            $res = db('auth_group')->data(array('status' => 1))->where('id=' . $id)->update();
        }
        if ($res) {
            //$this->success('恭喜，更新状态成功！');
			return json(array('code' => 200, 'msg' => '恭喜，更新状态成功！'));
        } else {
            $this->error('更新失败！');
        }
    }
	
	
	
}
?>